package club.banyuan.hrms.dao.impl;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.domain.Admin;
import club.banyuan.hrms.util.DbUtil;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
//@Scope("prototype")
// 优先级解决冲突 primary表示首先运用
@Primary
public class AdminDbDaoImpl implements AdminDao {
    @Override
    public List<Admin> getAdmins() {
        List<Admin> admins = new ArrayList<>();
        String sql = "SELECT * FROM t_admin";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()
        ) {
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String rolename = resultSet.getString("rolename");

                Admin admin = new Admin();
                admin.setId(id);
                admin.setUsername(username);
                admin.setRolename(rolename);

                admins.add(admin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public Admin getAdminByUsernameAndPassword(String username, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 获取链接
            connection = DbUtil.getConnection();
            // 执行sql语句
            String sql = "SELECT * FROM t_admin WHERE username=? AND password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();

            // 处理结果，提取admin
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                String rolename = resultSet.getString("rolename");

                Admin admin = new Admin();
                admin.setId(id);
                admin.setUsername(username);
                admin.setPassword(password);
                admin.setRolename(rolename);

                return admin;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    //    @Override
//    public Admin getAdminByUsernameAndPasswordWithStatement(String username, String password) {
//        Connection connection = null;
//        Statement statement = null;
//        ResultSet resultSet = null;
//
//        try {
//            // 获取链接
//            connection = DbUtil.getConnection();
//            // 执行sql语句
//            statement = connection.createStatement();
//            String sql = "SELECT * FROM t_admin WHERE username='" + username + "' AND password='" + password + "'";
//            resultSet = statement.executeQuery(sql);
//            // 处理结果，提取admin
//            while (resultSet.next()) {
//                Integer id = resultSet.getInt("id");
//                String rolename = resultSet.getString("rolename");
//
//                Admin admin = new Admin();
//                admin.setId(id);
//                admin.setUsername(username);
//                admin.setPassword(password);
//                admin.setRolename(rolename);
//
//                return admin;
//            }
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            // 关闭资源
//            if (resultSet != null) {
//                try {
//                    resultSet.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            if (statement != null) {
//                try {
//                    statement.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            if (connection != null) {
//                try {
//                    connection.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return null;
//    }

    @Override
    public void addAdmin(Admin admin) {
        String sql = "INSERT INTO t_admin(username,rolename,password) VALUES(?,?,?)";
        ResultSet resultSet = null;
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
        ) {

            preparedStatement.setString(1, admin.getUsername());
            preparedStatement.setString(2, admin.getRolename());
            preparedStatement.setString(3, admin.getPassword());

            Integer rows = preparedStatement.executeUpdate();
            resultSet = preparedStatement.getGeneratedKeys();
            while (resultSet.next()) {
//                Integer id = resultSet.getInt("id");
                Integer id = resultSet.getInt(1);
                admin.setId(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void updateAdmin(Admin admin) {
        String sql = "UPDATE t_admin SET username=?, rolename=?, password=? WHERE id=?";
        ResultSet resultSet = null;
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {

            preparedStatement.setString(1, admin.getUsername());
            preparedStatement.setString(2, admin.getRolename());
            preparedStatement.setString(3, admin.getPassword());
            preparedStatement.setInt(4, admin.getId());
            Integer rows = preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Admin getAdminById(Integer id) {
        String sql = "SELECT * FROM t_admin WHERE id=?";
        ResultSet resultSet = null;
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            preparedStatement.setInt(1, id);

            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String rolename = resultSet.getString("rolename");

                Admin admin = new Admin();
                admin.setId(id);
                admin.setUsername(username);
                admin.setPassword(password);
                admin.setRolename(rolename);

                return admin;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public void deleteAdmin(Integer[] ids) {
        String sql = "DELETE FROM t_admin WHERE id IN (?";
        for (int i = 1; i < ids.length; i++) {
            sql += ",?";
        }
        sql += ")";

        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            for (int i = 0; i < ids.length; i++) {
                preparedStatement.setInt(i + 1, ids[i]);
            }
            Integer rows = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
